require "ttWxCommon"

LIGHT_GREEN = Colour.new(0,0x7f,0)
LIGHT_RED_GREEN = Colour.new(0x7f,0x7f,0)
GREY = Colour.new(0x7f,0x7f,0x7f)

$globalCLexicalStyle =
{
	STC_C_DEFAULT => BLACK,
	STC_C_COMMENT => LIGHT_GREEN,
	STC_C_COMMENTLINE => LIGHT_GREEN,
	STC_C_COMMENTDOC => LIGHT_GREEN,
	STC_C_NUMBER => RED,
	STC_C_WORD => BLUE,
	STC_C_STRING => GREY,
	STC_C_CHARACTER => RED,
	STC_C_UUID => RED,
	STC_C_PREPROCESSOR => LIGHT_RED_GREEN,
	STC_C_OPERATOR => BLUE,
	STC_C_IDENTIFIER => BLACK,
	STC_C_STRINGEOL => RED,
	STC_C_VERBATIM => RED,
	STC_C_REGEX => LIGHT_GREY,
	STC_C_COMMENTLINEDOC => RED,
	STC_C_WORD2 => BLUE,
	STC_C_COMMENTDOCKEYWORD => LIGHT_GREEN,
	STC_C_COMMENTDOCKEYWORDERROR => LIGHT_GREEN,
	STC_C_GLOBALCLASS => CYAN
}
$globalCKeyWord =
 [
	"and","and_eq","asm","auto","bitand","bitor","bool",
	"break","case","catch","char","class","compl","const",
	"const_cast","continue","default","delete","do","double",
	"dynamic_cast","else","enum","explicit","export","extern",
	"false","float","for","friend","goto","if","inline","int",
	"long","mutable","namespace","new","not","not_eq","operator",
	"or","or_eq","private","protected","public","register",
	"reinterpret_cast","return","short","signed","sizeof","static",
	"static_cast","struct","switch","template","this","throw","true",
	"try","typedef","typeid","typename","union","unsigned","using",
	"virtual","void","volatile","wchar_t","while","xor","xor_eq"
]

class TTTextEditor < StyledTextCtrl
	@@sciMgr = {}
	def initialize(book,elem,type)
		@book = book
		@filePath = elem.attributes["path"]
		@uuid = elem.attributes["id"]
		@name = elem.attributes["name"]
		@type = type
		@modified = false
		@readPath = nil
		if nil == @@sciMgr[@uuid] then 
			super(@book)
			loadTextStyle()
			loadTextFile()
			@@sciMgr[@uuid] = self
			@book.add_page(self,@name,true)
			evt_stc_charadded self,:onCharadded
			evt_stc_change self,:onChange
		else
			@@sciMgr[@uuid].active()
		end
	end
	def active()
		index = @book.get_page_index(self)
		@book.set_selection(index)
	end
	def close()
		if true == @modified then
			dlg = MessageDialog.new(self,"Save file","message",YES_NO)
			select= dlg.show_modal
			if ID_YES == select then
				text = get_text()
				if @readPath then
					File.open(@readPath,"w") do |fp| 
						fp.write(text)
					end
				end
			end
		end
		@@sciMgr.delete(@uuid)
	end
######################################################################
	def loadTextStyle()
		font = Font.new(12, DEFAULT, NORMAL, NORMAL)
		style_set_font(STC_STYLE_DEFAULT, font)
		set_eol_mode(STC_EOL_LF)
		tmppath = Pathname.new(@filePath)
		type = tmppath.extname()
		if /head|cstd|cpp|cl/ =~ matchFileType(type) then
			loadCppStyle()
		end
		set_margin_width(1, 20)
		set_margin_type(1, STC_MARGIN_NUMBER)
	end
	def loadCppStyle()
		set_lexer(STC_LEX_CPP)
		style_clear_all
		$globalCLexicalStyle.each do |key,value|
			style_set_foreground(key, value)
		end
		keyWords=""
		$globalCKeyWord.each do |word|
			keyWords = keyWords + word + " "
		end
		set_key_words(0,keyWords)
	end
	def loadTextFile()
		locaText = ""
		fullPath = Pathname.new(@filePath)
		homePath = nil
		if $globalProjectHome then
			homePath = Pathname.new( $globalProjectHome + name)
		end
		if homePath and homePath.exist?() then
			@readPath = homePath
		end
		if fullPath and fullPath.exist?() then
			@readPath = fullPath
		end
		if @readPath and @readPath.exist?() then
			File.open(@readPath) do |fp| 
				fp.each do |line| 
					locaText = locaText + line
				end
			end
		else
			@readPath = nil
		end
		add_text(locaText)
	end
	def onCharadded(evt)
	end
	def onChange(evt)
		@modified = true
		index = @book.get_page_index(self)
		title = @book.get_page_text(index)
		if not title =~ /\*/
			title.gsub!(/$/,"*")
			@book.set_page_text(index,title)
		end
	end
end
